<?php

namespace app\admin\controller;

use app\admin\controller\Base;
use app\admin\model\Monitor;
use app\admin\model\Sensor;
use think\Request;
use think\paginator;


class System extends Base
{
    public function __constructor() {
        parent::__construct();
    }

    //测点定义界面
    public function monitor_define(){
        if(session('userinfo')){
            // $monitor=(new Monitor())->paginate(7);
            // $this->assign('monitor',$monitor);
            return $this->fetch();
        }else{
            return $this->error('请登录',url('admin/admin/login'));
        }
    }

    public function renderLoop()
    {
        $info = [];
        foreach (Monitor::all() as $loop){
            $data=["monitor_id"=>$loop['monitor_id'],
                "loop_name"=>$loop['loop_name'],
                "oprator"=>''];
            array_push($info,$data);
        }
        return [
            'data'=>$info
        ];
    }

    //删除测点
    public function deleteDefinedLoop()
    {
        $id = $_POST['id'];
        $res1 = Monitor::destroy(['monitor_id'=>$id]);
        $res2 = Sensor::destroy(['monitor_id'=>$id]);
        if($res1&&$res2){
            return true;
        }else{
            return false;
        }
    }
    //查看测点
    public function viewDefinedLoop($id)
    {
        $id = $_POST['id'];
        $monitorInfo = Monitor::get(['monitor_id'=>$id]);
        $sensorInfo = Sensor::get(['monitor_id'=>$id]);
        return [$monitorInfo,$sensorInfo];
    }
    //编辑测点
    public function editDefinedLoop()
    {
        $monitorInfo = $_POST['monitorInfo'];
        $sensorInfo = $_POST['sensorInfo'];
        $res1 = Monitor::update($monitorInfo,['monitor_id'=>$monitorInfo['monitor_id']]);
        if($res1){
            $res2 = Sensor::update($sensorInfo,['monitor_id'=>$sensorInfo['monitor_id']]);
            if($res2){
                return true;
            }
        }
        return false;
    }
    //新增测点
    public function addLoop()
    {
        $monitorInfo = $_POST['monitorInfo'];
        $sensorInfo = $_POST['sensorInfo'];
        $res1 = Monitor::create($monitorInfo);
        if($res1){
            $res2 = Sensor::create($sensorInfo);
            if($res2){
                return true;
            }
        }
        return false;
    }
}